import pylab import random import math x_inner, y_inner = [], [] x_outer, y_outer = [], [] for attempt in range(10000): a, b = random.uniform(-1.0, 1.0), random.uniform(-1.0, 1.0) if math.sqrt(a**2 + b**2) < 1.0: x_inner.append(a) y_inner.append(b) else: x_outer.append(a) y_outer.append(b) pylab.scatter(x_inner, y_inner,c= 'red', marker = '.', s=200) pylab.scatter(x_outer, y_outer,c= 'blue', marker = '.', s=200) print 4*len(x_inner)/float(len(x_inner) + len(x_outer)), math.pi pylab.axis('equal') pylab.title("children's game, with color code for pebbles") pylab.savefig('direct_pi_color.png') pylab.show()
You need to enable Javascript in your browser to edit pages.
help on how to format text